<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%--发送试卷生成在线考试答题流程--%>
<%
    JSONObject jsonData = new JSONObject();
    String id = Util.null2String(request.getParameter("id"));
    String curdate = DateUtil.getCurrentDate();
    String formtable = "formtable_main_286";
    String kskssj = DateUtil.getFullDate().substring(0, 16);
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
    try {
        ResourceComInfo comInfo = new ResourceComInfo();
        rs.executeQuery("select * from uf_sjtz where id=? and sfyfs=0", id);
        if (rs.next()) {
            int cjr = Util.getIntValue(rs.getString("cjr"));
            String cjrq = Util.null2String(rs.getString("cjrq"));
            String sjmc = Util.null2String(rs.getString("sjmc"));
            String ktszlx = Util.null2String(rs.getString("ktszlx"));
            String yjr = Util.null2String(rs.getString("yjr"));
            String ksscfz = Util.null2String(rs.getString("ksscfz"));
            String ksjssj = Util.null2String(rs.getString("ksjssj"));
            String zf = Util.null2String(rs.getString("zf"));
            String jgfs = Util.null2String(rs.getString("jgfs"));
            String cjksry = Util.null2String(rs.getString("cjksry"));
            String lcck = Util.null2String(rs.getString("lcck"));
            String[] arr = cjksry.split(",");
            int count = 0;
            for (int i = 0; i < arr.length; i++) {
                String userid = arr[i];
                String username = comInfo.getLastname(userid);
                String requestname = "在线考试答题-" + username + "-" + curdate;
                Map<String, String> requestMainMap = new HashMap<>();
                requestMainMap.put("dtr", userid);
                requestMainMap.put("cjr", cjr + "");
                requestMainMap.put("gs", comInfo.getSubCompanyID(userid));
                requestMainMap.put("bm", comInfo.getDepartmentID(userid));
                requestMainMap.put("cjrq", cjrq);
                requestMainMap.put("sjmc", sjmc);
                requestMainMap.put("ktszlx", ktszlx);
                requestMainMap.put("kszt", "0");
                requestMainMap.put("sfzk", "1");
                if (!"".equals(yjr)) {
                    requestMainMap.put("yjr", yjr);
                }
                requestMainMap.put("kssc", ksscfz);
                requestMainMap.put("kskssj", kskssj);
                requestMainMap.put("ksjssj", ksjssj);
                requestMainMap.put("zf", zf);
                requestMainMap.put("jgfs", jgfs);
                requestMainMap.put("cjksry", cjksry);
                requestMainMap.put("jmsjid", id);
                requestMainMap.put("kslx", "0");
                requestMainMap.put("sjsplc", lcck);
                String requestid = WorkflowCreateByRequestMap(userid, "133", requestname, "1", requestMainMap);
                if (Util.getIntValue(requestid) > 0) {
                    rs.executeQuery("select id,dtr,sjsplc from " + formtable + " where requestid=?", requestid);
                    rs.next();
                    String mainid = Util.null2String(rs.getString("id"));
                    String dtr = Util.null2String(rs.getString("dtr"));
                    String sjsplc = Util.null2String(rs.getString("sjsplc"));
                    String jmsjid = getIdToRequestid(sjsplc, "uf_kstjtz");
                    baseBean.writeLog(">>>>>getHxKstjtzmx>>>>>jmsjid=" + jmsjid);
                    String sql = "insert into uf_kstjtz_dt1(mainid,dtr,sjxq,kszt,sfzk,syks) values(?,?,?,?,?,?)";
                    rs.executeUpdate(sql, jmsjid, dtr, requestid, "0", "1", "所有考试");
                    String[] strs = new String[]{"1", "2", "3", "4"};
                    for (int j = 0; j < strs.length; j++) {
                        rs1.executeQuery("select tm,tmwb,fz,tmzf,tmxxnr,ROW_NUMBER() over(order by px) px from uf_sjtz_dt" + strs[j] + " where mainid=? order by px", id);
                        while (rs1.next()) {
                            String tm = Util.null2String(rs1.getString("tm"));
                            String fz = Util.null2String(rs1.getString("fz"));
                            String px = Util.null2String(rs1.getString("px"));
                            String tmwb = Util.null2String(rs1.getString("tmwb"));
                            String tmxxnr = Util.null2String(rs1.getString("tmxxnr"));
                            String tmzf = Util.null2String(rs1.getString("tmzf"));
                            rs1.executeUpdate("insert into formtable_main_286_dt" + strs[j] + " (mainid,tm,fz,px,tmwb,tmzf,tmxxnr) values(?,?,?,?,?,?,?)", mainid, tm, fz, px,tmwb,tmzf,tmxxnr);
                        }
                    }
                    count++;
                }
            }
            if (arr.length == count) {
                rs.executeUpdate("update uf_sjtz set sjzt=1,kszt=0,sfzk=1,kskssj='" + kskssj + "' where id=?", id);
                rs.executeUpdate("update uf_kstjtz set fssjzt=0,kskssj='" + kskssj + "',kszt=0 where lcck=?", lcck);
                rs.executeUpdate("update formtable_main_295 set sjzt=1,kskssj='" + kskssj + "' where requestid=?", lcck);
            }
            baseBean.writeLog("arr.length=" + arr.length);
            baseBean.writeLog("count=" + count);
            jsonData.put("status", arr.length == count);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>

<%!
    BaseBean baseBean = new BaseBean();

    /**
     * 创建流程方法
     */
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);//创建人Id
        requestInfo.setWorkflowid(workflowId);//工作流Id
        requestInfo.setDescription(requestName);//请求标题
        if ("0".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            p++;
        }
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest(requestInfo);//创建请求id
            } catch (Exception e) {
                baseBean.writeLog("=======" + e.getMessage());
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----姓名:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----时间:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----创建流程:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----请求:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:" + requestid);
            sbf.append("\r\n-----创建工作流记录日志结束");
            baseBean.writeLog(sbf.toString());
        } catch (Exception e) {
            baseBean.writeLog("错误：" + e);
        }
        return requestid;
    }

    /**
     * 字段转化
     */
    public static String getIdToRequestid(String requestid, String tablename) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + tablename + " where lcck=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }


%>




